﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MDT.LifeSewagePlatform.Mysql;
using MDT.LifeSewagePlatform.Repository;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.IRepository;
using System.Linq.Expressions;
using System;
using Microsoft.EntityFrameworkCore;

namespace MDT.LifeSewagePlatform.Repository
{
    public class AlarmRepository: BaseRepository<AlaramEntity>,IAlarmRepository
    {
        public AlarmRepository()
        {

        }

        public IQueryable<AlarmView> GetAlarms(Expression<Func<AlarmView,bool>> exp)
        {
                var query = from alarm in _dbContext.Alarm.AsNoTracking()
                                        join area in _dbContext.Regions.AsNoTracking() on alarm.AreaID equals area.ID
                                        join city in _dbContext.Regions.AsNoTracking() on alarm.CityID equals city.ID
                                        join company in _dbContext.Company.AsNoTracking() on alarm.CompanyID equals company.ID
                                        join change in _dbContext.Details.AsNoTracking() on alarm.ChangeTypeID equals change.ID
                                        select new AlarmView()
                                        {
                                            AlarmTypeID = alarm.AlarmTypeID,
                                            AreaID = alarm.AreaID,
                                            AreaName = alarm.MonitoringSite == "市" ? alarm.MonitoringSite : area.DisplayName,
                                            Msg = alarm.Msg,
                                            Date = alarm.Date,
                                            DutyID = alarm.DutyID,
                                            CityName = city.DisplayName,
                                            CompanyID = company.ID,
                                            ChangeTypeDesc = change.RowName,
                                            ChangeTypeID = change.RowID,
                                            MonitoringSite = alarm.MonitoringSite,
                                            Quarter=alarm.Quarter,
                                        };
            return query.Where(exp);
        }
    }
}
